package com.example.combwx.mvp.base.view;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.blankj.utilcode.util.BarUtils;
import com.example.combwx.R;
import com.example.combwx.mvp.base.presenter.IPresenter;
import com.example.combwx.utils.ToasetUtils;

import javax.inject.Inject;

public abstract class BaseFragment
        <P extends IPresenter> extends
        Fragment implements IView,IFragment {

    @Inject
    protected P p;

    protected View rootView;

    @SuppressLint("ResourceAsColor")
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(bindLayout(),container,false);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getActivity().getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            //把状态栏标记为浅色，然后状态栏的字体颜色自动转换为深色
        }
//        BarUtils.setStatusBarColor(getActivity(), addColor());
        //添加让出状态栏高度->防止contentView布局绘制到状态栏上
//        BarUtils.addMarginTopEqualStatusBarHeight(addStatusView());
        initView();
        inject();
        getLifecycle().addObserver(p);
        initData();
        return rootView;
    }
//    public abstract View addStatusView();
//    public abstract int addColor();
    @Override
    public void onDestroy() {
        super.onDestroy();
        getLifecycle().removeObserver(p);
    }

    public <T extends View> T f(int id){
        return rootView.findViewById(id);
    }

    @Override
    public void startPage(Bundle bundle, Class clazz) {
        Intent intent = new Intent(getActivity(), clazz);
        if (bundle!=null){
            intent.putExtras(bundle);
            getActivity().startActivity(intent);
        }else {
            getActivity().startActivity(intent);
        }
    }

    public void startPage(Class clazz){
        startPage(null,clazz);
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    @Override
    public void showMsg(String msg) {
        ToasetUtils.showMsg(msg);
    }
}
